package com.koron.css2.clientCenter.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.clientCenter.bean.ChangeUserImportBean;
import com.koron.css2.clientCenter.bean.UserChangeItemBean;
import com.koron.css2.clientCenter.mapper.ChangeUserImportMapper;
import com.koron.css2.clientCenter.mapper.ChangeUserMapper;
import com.koron.css2.serviceManage.bean.FieldComparisonBean;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.mapper.FieldComparisonMapper;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class ChangeUserImportRestore implements ServerInterface {

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		// TODO Auto-generated method stub
		
		ChangeUserImportBean queryBean = JsonUtils.objectToPojo(req.getData(), ChangeUserImportBean.class);
		try {
			FieldComparisonMapper fMapper = factory.getMapper(FieldComparisonMapper.class);
			ChangeUserImportMapper mapper = factory.getMapper(ChangeUserImportMapper.class);
			ChangeUserMapper changeUserMapper = factory.getMapper(ChangeUserMapper.class);
			//获取需要还原的字段
			List<FieldComparisonBean> comparisonList = fMapper.selectAllFieldComparison();
			//分开三个表区分开来设置
			List<FieldComparisonBean> userList = comparisonList.stream().filter(s->"user_info".equals(s.getTableName())).collect(Collectors.toList());;
			List<FieldComparisonBean> ctmList = comparisonList.stream().filter(s->"user_ctm".equals(s.getTableName())).collect(Collectors.toList());;
			List<FieldComparisonBean> accountList = comparisonList.stream().filter(s->"user_account".equals(s.getTableName())).collect(Collectors.toList());;
			//变更的值
			List<UserChangeItemBean> changeItem = mapper.selectChangeItem(queryBean.getId());
			StringBuffer sql = new StringBuffer();;
			List<UserChangeItemBean> items= new ArrayList<>();//还原变更记录数据
			String userNo=changeItem.get(0).getUserNo();
			for (int i=0;i<changeItem.size();i++) {
				UserChangeItemBean bean = changeItem.get(i);
				if(!userNo.equals(bean.getUserNo()) && i!=0) {
					//删除最后一个,
					sql.deleteCharAt(sql.length()-1);
					//每个用户执行一次sql
					mapper.restoreChangeImport(userNo, sql.toString());
					userNo=bean.getUserNo();
					sql = new StringBuffer();
				}
				String oldValue = bean.getOldItemValue();
				//为空时将旧值设置为空字符串
				if("空".equals(bean.getOldItemValue())||"无".equals(bean.getOldItemValue())) {
					oldValue=null;
				}else {
					oldValue="'"+bean.getOldItemValue()+"'";
				}
				//循环组装需要更新的字段
				for (FieldComparisonBean userItem : userList) {
					if(userItem.getColumnZh().equals(bean.getChangeItem())) {
						sql.append("b."+userItem.getColumnEn()+" = "+oldValue+",");
						break;
					}
				}
				for (FieldComparisonBean ctmItem : ctmList) {
					if(ctmItem.getColumnZh().equals(bean.getChangeItem())) {
						sql.append("d."+ctmItem.getColumnEn()+" = "+oldValue+",");
						break;
					}
				}
				for (FieldComparisonBean accountItem : accountList) {
					if(accountItem.getColumnZh().equals(bean.getChangeItem())) {
						sql.append("e."+accountItem.getColumnEn()+" = "+oldValue+",");
						break;
					}
				}
				//最后一个变更事项执行一下sql
				if(i==changeItem.size()-1) {
					sql.deleteCharAt(sql.length()-1);
					//每个用户执行一次sql
					mapper.restoreChangeImport(userNo, sql.toString());
				}
				/**
				 * 生成还原变更记录
				 */
				UserChangeItemBean userChangeItemBean = new UserChangeItemBean();
				userChangeItemBean.setCreateInfo(userInfo);
				userChangeItemBean.setReceiptType("批量变更还原");
				userChangeItemBean.setUserNo(changeItem.get(i).getUserNo());
				userChangeItemBean.setCtmNo(changeItem.get(i).getCtmNo());
				userChangeItemBean.setAccountNo(changeItem.get(i).getAccountNo());
				userChangeItemBean.setChangeItem(changeItem.get(i).getChangeItem());
				userChangeItemBean.setChangeCode(changeItem.get(i).getChangeCode());
				userChangeItemBean.setOldItemValue(changeItem.get(i).getNewItemValue());
				userChangeItemBean.setNewItemValue(changeItem.get(i).getOldItemValue());
				items.add(userChangeItemBean);
			}
			changeUserMapper.userChangeItemAdd(items,1);//插入还原变更记录
			return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "还原成功", null);
		}catch (Exception e) {
			// TODO: handle exception
			logger.error("批量变更还原失败" + e.getMessage(), e);
			return MessageBean.create(Constant.MESSAGE_INT_FAIL, "还原失败", null);
		}
	}
}
